﻿@{
    ViewBag.Title = ViewBag.Title;
}
@section headerSection{
    <style>
        .float-unit {
            height: 100%;
            border: 1px solid #E7EAEC;
            border-top-width: 0px;
            border-bottom-width: 0px;
            margin-bottom: 0px;
        }

        .left-sm-3 {
            padding-left: 0px;
        }

        .right-sm-9 {
            padding-right: 0px;
        }

        .ibox-unit {
            overflow-y: auto;
            height: 89%;
            padding: 0px;
        }

        #exampleToolbar div {
            margin-bottom: 0px;
        }

        #exampleToolbar > .form-group {
            float: left;
            margin: 0;
            margin-right: 5px;
            margin-top: 1px;
        }

        .form-group > button {
            margin-top: -2px;
            margin-bottom: 2px;
        }
    </style>
}

<div class="animated fadeInRight">
    <!-- Panel Other -->
    <div class="ibox float-e-margins" style="margin-bottom: 0px;">
        <div class="ibox-content" style="padding: 5px 20px 0px 20px;">
            <div class="row row-lg">
                <div class="col-sm-12 col-sm-leftRight">
                    <!-- Example Toolbar -->
                    <form id="searchForm" class="form-inline">
                        <div class="example">
                            <div class="btn-group hidden-xs" id="exampleToolbar" role="group">
                                <div class="form-group">
                                    <input name="Search" type="text" class="form-control  input-sm" placeholder="订单号/商品名称/购买人">
                                    <input type="hidden" id="page" name="page" />
                                    <input type="hidden" id="rows" name="rows" />
                                </div>
                                <div class="form-group">
                                    <select name="ProType" class="form-control input-sm" style="padding-bottom:4px;padding-top:4px;">
                                        <option value="">(产品类别)</option>
                                        @{
                                            foreach (var i in ViewBag.ProType)
                                            {
                                                <option value="@i.value">@i.text</option>
                                            }
                                        }
                                    </select>
                                </div>
                                <div class="form-group">
                                    <select name="PurchasePeriod" class="form-control input-sm" style="padding-bottom:4px;padding-top:4px;">
                                        <option value="">(商品期限)</option>
                                        <option value="1">1个月</option>
                                        <option value="3">3个月</option>
                                        <option value="6">6个月</option>
                                        <option value="12">一年</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <select name="PaymentMethod" class="form-control input-sm" style="padding-bottom:4px;padding-top:4px;">
                                        <option value="">(付款方式)</option>
                                        <option value="1">金币</option>
                                        <option value="2">微信</option>
                                        <option value="3">支付宝</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <select name="OrderState" class="form-control input-sm" style="padding-bottom:4px;padding-top:4px;">
                                        <option value="">(订单状态)</option>
                                        <option value="1">等待买家付款</option>
                                        <option value="2">买家已付款</option>
                                        @*<option value="3">卖家已确认</option>*@
                                        <option value="4">交易成功</option>
                                        <option value="5">交易关闭</option>
                                        <option value="6">退款中</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <select name="OrderSource" class="form-control input-sm" style="padding-bottom:4px;padding-top:4px;">
                                        <option value="">(订单来源)</option>
                                        <option value="pc">pc</option>
                                        <option value="web">web</option>
                                        <option value="mobile">mobile</option>
                                        <option value="wechat">wechat</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <input name="OrderCreateTimeStart" type="text" class="form-control input-sm"
                                           autocomplete="off" placeholder="起始下单时间" style="width:110px;"
                                           onclick="laydate({istime: true, format: 'YYYY-MM-DD'})" />

                                    <label class="control-label">-</label>
                                    <input name="OrderCreateTimeEnd" type="text" autocomplete="off"
                                           class="form-control input-sm" placeholder="截止下单时间" style="width:110px;"
                                           onclick="laydate({istime: true, format: 'YYYY-MM-DD'})" />
                                </div>
                                <div class="form-group">
                                    <button id="btnSearch" class="btn btn-primary btn-sm" type="button"><i class="fa fa-search"></i>&nbsp;查询</button>
                                </div>
                                <div class="form-group">
                                    <button id="resetForm" class="btn btn-primary btn-sm" type="button">
                                        <span class="glyphicon glyphicon-repeat"></span>
                                        重置
                                    </button>
                                </div>
                                <div class="form-group">
                                    <button id="expa" class="btn btn-primary btn-sm" type="button">
                                        <span class="glyphicon glyphicon-cloud-download"></span>
                                        导出
                                    </button>
                                </div>
                            </div>
                            <table id="exampleTableToolbar" data-mobile-responsive="true" data-radio="true"></table>
                        </div>
                    </form>
                    <!-- End Example Toolbar -->
                </div>
            </div>
        </div>
    </div>
    <!-- End Panel Other -->
</div>

@section footSection{
    <script src="~/Resources/tools/jqueryExtend.js"></script>
    <script>
        var orderType = @ViewBag.OrderType ;
        var UserID = '@ViewBag.UserId';
        var RequestUrl = '@ViewBag.ApiUrl';

        /*
        订单管理
        */

        function OrderIndex() {
            this.urlRoot = ctx + '/Order/';

            this.tableObj;

        }
        function Money(value, num) {
            num = num > 0 && num <= 20 ? num : 2;
            value = parseFloat((value + "").replace(/[^\d\.-]/g, "")).toFixed(num) + ""; //将金额转成比如 123.45的字符串
            var valueArr = value.split(".")[0].split("") //将字符串的数变成数组
            const valueFloat = value.split(".")[1]; // 取到 小数点后的值
            let valueString = "";
            for (let i = 0; i < valueArr.length; i++) {

                valueString += valueArr[i] + ((i + 1) % 3 == 0 && (i + 1) != valueArr.length ? "," : ""); //循环 取数值并在每三位加个','

            }
            const money = valueString.split("").join("") + "." + valueFloat; //拼接上小数位
            return '￥'+money
        }


        OrderIndex.prototype.initTable = function () {
            var This = this;
            var options = {
                url: This.urlRoot + "OrderList",
                cutHeight: 5,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                toolbar: "#exampleToolbar",
                queryParams: function (params) {
                    $('#page').val(params.offset / params.limit + 1);
                    $('#rows').val(params.limit);

                    var paramData = $('#searchForm').serializeJson();
                    paramData.OrderType = orderType;
                    return paramData;
                },
                responseHandler: function (server) {
                    if (server.Statu == 0) {
                        return server.Data;
                    } else {
                        layer.alert('获取订单数据失败', { icon: 5 });
                    }
                },
                columns: [
                    {
                        radio: true
                    },
                    {
                        field: '',//第一列序号
                        title: '操作',
                        align: 'center',
                        width: 50,
                        formatter: function (value, row, index) {
                            if (row.OrderState == 2 && orderType!=1) {
                                var b = '<button class="btn btn-info btn-circle btn-outline okOrder" onclick="OkOrder();" type="button" OrderCode="' + row.OrderCode + '" title="确认订单"><span class="glyphicon glyphicon-ok"></span></button>';
                                return b;
                            }
                            return '';
                        }
                    },
                    {
                        field: 'OrderCode',
                        title: '订单号',
                        align: 'center'
                    },
                    {
                        field: 'ProType',
                        title: '产品类别',
                        align: 'center',
                        formatter: function (v, r, i) {
                            if (v == 1) {
                                return 'VRPPT';
                            }
                            if (v == 2) {
                                return '创客';
                            }
                            if (v == 3) {
                                return '头记';
                            }
                            if (v == 4) {
                                return '校史馆';
                            }
                        }
                    },
                    {
                        field: 'FileName',
                        title: '商品名称',
                        align: 'center'
                    },
                    {
                        field: 'CreateFileUser',
                        title: '作者',
                        align: 'center'
                    }, {
                        field: 'OrderCreateTime',
                        title: '下单时间',
                        align: 'center'
                    }, {
                        field: 'OrderCreateUser',
                        title: '购买人',
                        align: 'center'
                    },
                    {
                        field: 'PurchasePeriod',
                        title: '商品期限',
                        align: 'center',
                        formatter: function (v, r, i) {
                            if (v == 1) {
                                return '1个月';
                            }
                            if (v == 3) {
                                return '3个月';
                            }
                            if (v == 6) {
                                return '6个月';
                            }
                            if (v == 12) {
                                return '一年';
                            }
                        }
                    }
                    ,
                    {
                        field: 'CommodityMoney',
                        title: '商品金额',
                        align: 'center',
                        formatter: function (v, r, i) {
                            return Money(v,2);
                        }
                    }
                    ,
                    {
                        field: 'DiscountMoney',
                        title: '折后金额',
                        align: 'center',
                        formatter: function (v, r, i) {
                            return Money(v,2);
                        }
                    }
                    ,
                    {
                        field: 'PreferentialAmount',
                        title: '优惠金额',
                        align: 'center',
                        formatter: function (v, r, i) {
                            return Money(v,2);
                        }
                    }
                    ,
                    {
                        field: 'PaymentAmount',
                        title: '实付金额',
                        align: 'center',
                        formatter: function (v, r, i) {
                            return Money(v,2);
                        }
                    }
                    ,
                    {
                        field: 'PaymentMethod',
                        title: '付款方式',
                        align: 'center',
                        formatter: function (v, r, i) {
                            if (v == 1) {
                                return '金币';
                            }
                            if (v == 2) {
                                return '微信';
                            }
                            if (v == 3) {
                                return '支付宝';
                            }
                        }
                    }
                    ,
                    {
                        field: 'PaymentTime',
                        title: '付款时间',
                        align: 'center'
                    }
                    ,
                    {
                        field: 'ConfirmTime',
                        title: '确认日期',
                        align: 'center'
                    }
                    ,
                    {
                        field: 'EndTime',
                        title: '截止日期',
                        align: 'center'
                    }
                    ,
                    {
                        field: 'OrderState',
                        title: '订单状态',
                        align: 'center',
                        formatter: function (v, r, i) {
                            //1等待买家付款、2买家已付款、3卖家已确认、4交易成功、5交易关闭、6退款中
                            if (v == 1) {
                                return '等待买家付款';
                            }
                            if (v == 2) {
                                return '买家已付款';
                            }
                            //if (v == 3) {
                            //    return '卖家已确认';
                            //}
                            if (v == 4) {
                                return '交易成功';
                            }
                            if (v == 5) {
                                return '交易关闭';
                            }
                            if (v == 6) {
                                return '退款中';
                            }
                        }
                    }
                    ,
                    {
                        field: 'OrderSource',
                        title: '订单来源',
                        align: 'center'
                    }
                    ,
                    {
                        field: 'PaymentCode',
                        title: '支付流水号',
                        align: 'center'
                    }
                    ,
                    {
                        field: 'ParentOrderCode',
                        title: '父订单号',
                        align: 'center'
                    }
                    ,
                    {
                        field: 'OrderSubmitUser',
                        title: '提单人',
                        align: 'center'
                    }
                    ,
                    {
                        field: 'Area',
                        title: '区域',
                        align: 'center'
                    }
                    ,
                    {
                        field: 'TableNum',
                        title: '桌号',
                        align: 'center'
                    }
                    ,
                    {
                        field: 'Quantity',
                        title: '数量',
                        align: 'center'
                    }
                    ,
                    {
                        field: 'Name',
                        title: '收货人',
                        align: 'center'
                    }
                ]
            }
            This.tableObj = tableHelper.initTable("exampleTableToolbar", options);
        }

        OrderIndex.prototype.reloadTable = function () {
            this.tableObj.bootstrapTable('refresh');
        }

        OrderIndex.prototype.getSelectRow = function () {
            var selectRows = this.tableObj.bootstrapTable('getSelections');
            if (selectRows.length == 0) {
                layer.alert('请选择一条订单', { icon: 6 });
                return null;
            }
            return selectRows[0];
        }

        OrderIndex.prototype.bind = function () {
            var This = this;

            $('#resetForm').click(function () {
                $('#searchForm')[0].reset();
                This.reloadTable();
            });
            $('#btnSearch').click(function () {
                This.reloadTable();
            });

            $('#exampleTableToolbar').on('click', '.okOrder', function () {
                var orderCode = $(this).attr('OrderCode');

                layer.open({
                    content: '是否确认该订单？',
                    btn: ['确定', '关闭'],
                    icon: 1,
                    yes: function (index, layero) {
                        var data = {
                            "OrderCode": orderCode,
                            "userid": UserID
                        };
                        var loadIndex;
                        $.ajax({
                            url: RequestUrl + 'api/Pay/ConfirmOrder',
                            type: 'post',
                            data: JSON.stringify(data),
                            contentType: "application/json",
                            beforeSend: function () {
                                loadIndex = layer.msg('订单确认中...', { icon: 16, shade: [0.3, '#393D49'], time: 0 });
                            },
                            success: function (server) {
                                layer.close(loadIndex);
                                if (server.errcode == 0) {
                                    layer.msg('确认订单成功', { icon: 1 });
                                    This.reloadTable();
                                } else {
                                    layer.alert('确认订单成功，出现错误：' + server.errmsg, { icon: 5 });
                                }
                            },
                            error: function () {
                                layer.close(loadIndex);
                                layer.msg('服务器繁忙请稍后再试！', { icon: 5 });
                            }
                        });
                    }
                });
            });

            $('#expa').click(function () {
                var paramData = $('#searchForm').serializeJson();
                paramData.OrderType = orderType;
                var loadIndex;
                $.ajax({
                    url: This.urlRoot + "exp",
                    type: 'post',
                    data: JSON.stringify(paramData),
                    contentType: "application/json",
                    beforeSend: function () {
                        loadIndex = layer.msg('导出文件生成中...', { icon: 16, shade: [0.3, '#393D49'], time: 0 });
                    },
                    success: function (server) {
                        layer.close(loadIndex);
                        if (server.Statu == 0) {
                            layer.msg('导出文件成功，准备下载', { icon: 1 });
                            window.open(ctx + server.Data);
                        } else {
                            layer.alert('导出文件生成，出现错误：' + server.errmsg, { icon: 5 });
                        }
                    },
                    error: function () {
                        layer.close(loadIndex);
                        layer.msg('服务器繁忙请稍后再试！', { icon: 5 });
                    }
                });
            });
        }

        $(function () {
            var shopping = new OrderIndex();
            shopping.bind();
            shopping.initTable();
        });

        //function OkOrder() {
        //    var orderCode = $(this).attr('OrderCode');

        //    layer.open({
        //        content: '是否确认该订单？',
        //        btn: ['确定', '关闭'],
        //        icon: 1,
        //        yes: function (index, layero) {
        //            var data = {
        //                "OrderCode": orderCode,
        //                "userid": UserID
        //            };
        //            var loadIndex;
        //            $.ajax({
        //                url: RequestUrl + 'api/Pay/ConfirmOrder',
        //                type: 'post',
        //                data: JSON.stringify(data),
        //                contentType: "application/json",
        //                beforeSend: function () {
        //                    loadIndex = layer.msg('订单确认中...', { icon: 16, shade: [0.3, '#393D49'], time: 0 });
        //                },
        //                success: function (server) {
        //                    layer.close(loadIndex);
        //                    if (server.errcode == 0) {
        //                        layer.msg('确认订单成功', {icon:1});
        //                    } else {
        //                        layer.alert('确认订单成功，出现错误：' + server.errmsg, { icon: 5 });
        //                    }
        //                },
        //                error: function () {
        //                    layer.close(loadIndex);
        //                    layer.msg('服务器繁忙请稍后再试！', {icon:5});
        //                }
        //            });
        //        }
        //    });
        //}
    </script>
}
